package util

import (
	"os"
	"os/signal"
	"syscall"
	"time"
)

type signalHandler func(s os.Signal, arg interface{})

func WaitSignal(sig signalHandler, arg interface{}) {

	c := make(chan os.Signal, 1)
	signal.Notify(c, syscall.SIGHUP, syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT)
	for {
		s := <-c
		Info("get a signal %s", s.String())
		switch s {
		case syscall.SIGQUIT, syscall.SIGTERM, syscall.SIGINT:
			sig(s, arg)
			time.Sleep(time.Second)
			return
		case syscall.SIGHUP:
		default:
			return
		}
	}
}

/*

	"snc_signalr"
	"syscall"
	"os"
	"os/signal"
	ss := snc_signalr.NewSignalController()
	handler := func(s os.Signal, arg interface{}) {
		fmt.Printf("handle signal: %v\n", s)
	}
	ss.Register(syscall.SIGINT, handler)
	ss.Register(syscall.SIGQUIT, handler)
	ss.Register(syscall.SIGKILL, handler)
	ss.Register(syscall.SIGHUP, handler)

	c := make(chan os.Signal, 1)

	var sigs []os.Signal
	for sig := range ss.SignalTable {
		sigs = append(sigs, sig)
	}
	signal.Notify(c)

*/
